package linklist;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/*
 *@description
 *@author hkfan
 *@version 1.0
 *@create 2025/6/9 16:50
 */
public class LeetCode_82 {
    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public ListNode deleteDuplicates(ListNode head) {
        //0. 特殊情况
        if(head==null || head.next==null){
            return head;
        }
        //1. 添加临时头节点
        ListNode tempHead = new ListNode(0,head);
        //2. 定义指针
        ListNode a = tempHead,b=a.next,c=b.next;
        //3. 循环
        while (b!=null && c!=null){
            if (b.val==c.val){
                while (c!=null && c.val==b.val){
                    c=c.next;
                }
                if (c==null){
                    a.next=c;
                    break;
                }
                b=c;c=b.next;
                if (c==null){
                    a.next=b;
                }
            }else {
                a.next=b;
                a=b;
                b=c;
                if (b==null){
                    break;
                }
                c=b.next;
            }
        }
        return tempHead.next;
    }




}
